﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentFTP.Configuration;
using FluentFTP.Model;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;

namespace FluentFTP.ViewModel
{
    internal class EditServerViewModel : NotificationObject
    {
        private const string ftpPrefix = "ftp://";

        private FtpServerInfo server = new FtpServerInfo { Id = Guid.NewGuid(), Adress = ftpPrefix };
        public FtpServerInfo Server
        {
            get { return server; }
            set
            {
                server = value;
                server.PropertyChanged += (_, args) => Save.RaiseCanExecuteChanged();
            }
        }

        public DelegateCommand Save { get; private set; }

        public EditServerViewModel()
        {
            Save = new DelegateCommand(() =>
                {
                    var existingServer = Settings.Servers.FirstOrDefault(x => x.Id == server.Id);
                    if (existingServer != null) existingServer.CopyTo(Server);
                    else Settings.Servers.Add(Server);
                }
                //() => Server != null && server.Error == string.Empty
                );
        }

        public EditServerViewModel(FtpServerInfo server)
            : this()
        {
            Server = server;
        }

    }
}
